Numpy Random Number Generation: A Beginner's Guide to rand and randn

NumPy is the core library for scientific computing in Python. The `np.random` submodule provides random number generation functionality, with `rand` and `randn` being commonly used functions. These random numbers are pseudo-random, and fixing the seed allows for reproducible results. `np.random.rand(d0, …, dn)` generates random numbers from a **uniform distribution over [0, 1)**. The parameters specify the array shape (e.g., 1-dimensional, 2-dimensional, etc.), and all elements lie within [0, 1). It is suitable for scenarios requiring equal probability values (e.g., initializing weights). `np.random.randn(d0, …, dn)` generates random numbers from a **standard normal distribution** (mean 0, standard deviation 1). Elements are concentrated between -1 and 1, with a low probability of extreme values. To adjust the mean and standard deviation, the formula `μ + σ * randn` can be used. This is often applied to simulate natural data fluctuations (e.g., noise). Both functions accept shape parameters, with the former producing uniform distribution and the latter normal distribution. The results can be reproduced by fixing the seed using `np.random.seed(seed)`.

Read More
Numpy Broadcasting: A Core Technique to Simplify Array Operations

The Numpy broadcasting mechanism addresses element-wise operations for arrays of different shapes by automatically expanding smaller arrays to match the shape of larger arrays and aligning dimensions, eliminating the need for manual reshaping, thus saving memory and improving efficiency. Core rules: dimensions are matched from right to left, with each dimension size either being 1 or equal; smaller arrays are broadcasted to the merged shape of the larger array. For example, scalars (e.g., 10) can be broadcast to any array shape; when a 1D array (e.g., [10, 20, 30]) is broadcasted with a 2×3 2D array, the 1D array is repeated into 2 rows. When a 3D array (2×2×2) is broadcasted with a 2×2 2D array, the 2D array is expanded to 2×2×2. If dimensions are incompatible (e.g., 2×2 and 1×3), an error is raised. Practical applications include element-wise operations (e.g., adding a constant to an array) and matrix standardization, avoiding loops and simplifying code. Mastering broadcasting significantly enhances the efficiency and readability of Numpy array operations.

Read More